﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UFIDA.U8.YYNBJY
{
    public partial class FRMain : Form
    {
        public static FRMain form1;
        public static BindingSource source = new BindingSource();
        public FRMain()
        {
            InitializeComponent();
            form1 = this;
        }
        private delegate void delInfoList(string text);
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FRMain_Load(object sender, EventArgs e)
        {
            
        }
       
        /// <summary>
        /// 手工同步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BTNPOTB_Click(object sender, EventArgs e)
        {
            string filename = "生产订单";
            this.label2.Text = "同步中";
            bool iilog = false;
            if (logcheck.Checked)
            {
                iilog = true;
            }
            string mocode = this.textBox1.Text.Trim();
            string DBNAME = Model.PUBCLASS.DBNAME;
            string[] DB = DBNAME.Split('|');
            for (int i = 0; i < DB.Length; i++)
            {
                DBNAME = DB[i].Trim();
                Operate.TBMOMORDER.TBMOM(filename,DBNAME,mocode,DBHelp.LogException.ilog,iilog);
            }
        }
        /// <summary>
        /// 定时确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BTNQD_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtds.Text.Trim()) && this.txtds.Text.Trim() != "0")
            {
                int dstime = Convert.ToInt32(this.txtds.Text.Trim());
                this.dstimer.Interval = dstime * 60 * 1000;
                MessageBox.Show("时间设置成功!10秒后执行", "提示");
                PUBMethod.PMethod.SuperSleep(10);
                dstimer_Tick(null, null);

            }
            else
            {
                MessageBox.Show("时间不可为空或者零,已默认为5分钟", "提示");
                this.dstimer.Interval = 5 * 60 * 1000;
            }
        }
        /// <summary>
        /// 定时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dstimer_Tick(object sender, EventArgs e)
        {
            this.label2.Text = "启动";
            PUBMethod.PMethod.SuperSleep(10);
            this.dstimer.Enabled = true;
            bool iilog = this.logcheck.Checked;
            if (!string.IsNullOrEmpty(this.txtds.Text.Trim()))
            {
                int dstimes = Convert.ToInt32(this.txtds.Text.Trim());
                this.dstimer.Interval = dstimes * 60 * 1000;
            }
            else
            {
                this.dstimer.Interval = 5 * 60 * 1000;
            }
            PUBMethod.PMethod.printmsgs("定时", "=========["+DateTime.Now.ToString()+ "]启动=========", true, false, false, System.Drawing.Color.Red);
            string filename = "定时任务凭证";
            string DBNAME = Model.PUBCLASS.DBNAME;
            string[] DB = DBNAME.Split('|');
            for (int i = 0; i < DB.Length; i++)
            {
                DBNAME = DB[i].Trim();
                string date = DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01";
                Operate.TBGL_ACCVOUCH.GL_ACCVOUCH(filename,date, DBNAME, "","", DBHelp.LogException.ilog, iilog);
            }
        }
        /// <summary>
        /// 定时文本框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtds_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//删除
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//0-9
                {
                    e.Handled = true;
                }
            }
        }

        private void BTNFIRST_Click(object sender, EventArgs e)
        {
        }


        private void BTNMK_Click(object sender, EventArgs e)
        {
            string filename = "凭证同步";
            this.label2.Text = "同步中";
            bool iilog = false;
            if (logcheck.Checked)
            {
                iilog = true;
            }
            string date=dateTimePicker1.Value.ToString("yyyy-MM")+"-01";
            
            string ino_id = this.textBox1.Text.Trim();//凭证号
            string PZTYPE=this.textBox2.Text.Trim();//凭证类型
            string DBNAME = Model.PUBCLASS.DBNAME;
            string[] DB = DBNAME.Split('|');
            for (int i = 0; i < DB.Length; i++)
            {
                DBNAME = DB[i].Trim();
                Operate.TBGL_ACCVOUCH.GL_ACCVOUCH(filename,date, DBNAME, ino_id,PZTYPE, DBHelp.LogException.ilog, iilog);
            }
        }

        private void BTNCX_Click(object sender, EventArgs e)
        {
            string filename = "凭证查询";
            bool iilog = false;
            if (logcheck.Checked)
            {
                iilog = true;
            }
            string date = dateTimePicker1.Value.ToString("yyyy-MM") + "-01";
            string date2 = Convert.ToDateTime(date).AddMonths(1).ToString("yyyy-MM-dd");
            string ino_id = this.textBox1.Text.Trim();//凭证号
            string PZTYPE = this.textBox2.Text.Trim();//凭证类型
            string where = " and 1=1 ";
            if (!string.IsNullOrEmpty(ino_id))
            {
                where += " and ino_id like '%" + ino_id + "%'";
            }
            if (!string.IsNullOrEmpty(PZTYPE))
            {
                where += " and csign like '%" + PZTYPE + "%'";
            }
            string DBNAME = Model.PUBCLASS.DBNAME;
            string[] DB = DBNAME.Split('|');
            for (int i = 0; i < DB.Length; i++)
            {
                DBNAME = DB[i].Trim();

                string sql = @"select
                (CASE WHEN ISNULL(TVOUVHTIME,'')='' THEN '新增' ELSE '修改' END) [STATE],
                 GL.csign,GL.ino_id,GL.dbill_date,GL.iYPeriod,GL.tvouchtime,EF.TVOUVHTIME AS [TIME],TBDATE
                 from (
                select csign,ino_id,isignseq,iYPeriod,dbill_date,convert(varchar(16),max(tvouchtime),120) tvouchtime from GL_ACCVOUCH 
                WHERE  isnull(iflag,0)=0 and dbill_date>='" + date +@"' and dbill_date<'"+date2+@"' "+where+ @"
                group by csign,ino_id,isignseq,iYPeriod,dbill_date )GL 
                left join EF_GL_ACCVOUCH EF ON GL.csign=EF.CSIGN AND GL.ino_id=EF.INO_ID AND GL.isignseq=EF.ISIGNSEQ AND GL.iYPeriod=EF.IYPERIOD
                where GL.csign=EF.CSIGN AND GL.ino_id=EF.INO_ID AND GL.isignseq=EF.ISIGNSEQ AND GL.iYPeriod=EF.IYPERIOD 
                and ISNULL(GL.ino_id,'')<>'' and GL.tvouchtime<>convert(varchar(16),EF.TVOUVHTIME,120)";
                DBHelp.LogException.WriteiLog("查询凭证", sql, DBHelp.LogException.ilog, iilog);
                DataTable DT = DBHelp.SQLHelper.GetDataTable(sql);
                if (DT != null && DT.Rows.Count > 0)
                {
                    dataGridView2.DataSource =  DT;//绑定数据
                    label10.Text = DT.Rows.Count.ToString();
                }
                else
                {
                    MessageBox.Show("没有查询到数据", "提示");
                    dataGridView2.DataSource =null;//绑定数据
                    label10.Text = "0";
                }
            }
        }
    }
}
